set(DEFAULT_INSTALL_LAYOUT "STANDALONE")

set(INSTALL_LAYOUT "${DEFAULT_INSTALL_LAYOUT}"
CACHE STRING "Installation directory layout. Options are: STANDALONE DEB")

message(STATUS "Installation layout set to ${INSTALL_LAYOUT}")

if(INSTALL_LAYOUT STREQUAL "DEB")
  set(default_prefix "/usr")
  set(secure_file_priv_path "/var/lib/szarp")
else()
  set(default_prefix "/opt/szarp")
  set(secure_filre_priv_path "")
endif()

set(CMAKE_INSTALL_PREFIX ${default_prefix}
  CACHE PATH "install prefix" FORCE)

# DEB layout
set(INSTALL_BINDIR_DEB                       "bin")
set(INSTALL_SBINDIR_DEB                      "sbin")
set(INSTALL_SCRIPTDIR_DEB                    "bin")
set(INSTALL_LOGDIR_DEB                       "/var/log/szarp")
set(INSTALL_CONFIGDIR_DEB                    "/etc/szarp")
set(INSTALL_RUNTIMEDIR_DEB                   "/var/run/szarp")
set(INSTALL_DATADIR_DEB                      "/var/lib/szarp")
set(INSTALL_LIBDIR_DEB                       "lib/x86_64-linux-gnu")
#
set(INSTALL_INCLUDEDIR_DEB                   "include/szarp")
#
set(INSTALL_DOCDIR_DEB                       "share/szarp/docs")
set(INSTALL_DOCREADMEDIR_DEB                 "share/szarp/docs")
set(INSTALL_INFODIR_DEB                      "share/szarp/docs")
#
set(INSTALL_SHAREDIR_DEB                     "share")
set(INSTALL_SUPPORTFILESDIR_DEB              "share/szarp")
#
set(INSTALL_SECURE_FILE_PRIVDIR_DEB          ${secure_file_priv_path})

# STANDALONE layout
set(INSTALL_BINDIR_STANDALONE               "bin")
set(INSTALL_SBINDIR_STANDALONE              "bin")
set(INSTALL_SCRIPTDIR_STANDALONE            "bin")
set(INSTALL_LOGDIR_STANDALONE               "/var/log/szarp")
set(INSTALL_CONFIGDIR_STANDALONE            "/etc/szarp")
set(INSTALL_RUNTIMEDIR_STANDALONE           "run")
set(INSTALL_DATADIR_STANDALONE              "data")
set(INSTALL_LIBDIR_STANDALONE               "lib")
#
set(INSTALL_INCLUDEDIR_STANDALONE           "include")
#
set(INSTALL_DOCDIR_STANDALONE               "resources")
set(INSTALL_DOCREADMEDIR_STANDALONE         ".")
set(INSTALL_INFODIR_STANDALONE              "resources")
#
set(INSTALL_SHAREDIR_STANDALONE             "resources")
set(INSTALL_SUPPORTFILESDIR_STANDALONE      "resources")
#
set(INSTALL_SECURE_FILE_PRIVDIR_STANDALONE  ${secure_file_priv_path})

# clear cached variables
if(OLDINSTALL_LAYOUT)
  if(NOT OLD_INSTALL_LAYOUT STREQUAL INSTALL_LAYOUT)
    set(FORCE FORCE)
  endif()
endif()
set(OLD_INSTALL_LAYOUT ${INSTALL_LAYOUT} CACHE INTERNAL "")

foreach(var BIN SBIN SCRIPT LOG CONFIG RUNTIME DATA LIB INCLUDE DOC
    DOCREADME INFO SHARE SUPPORTFILES SECURE_FILE_PRIV)
  set(INSTALL_${var}DIR ${INSTALL_${var}DIR_${INSTALL_LAYOUT}}
    CACHE STRING "${var} installation directory" ${FORCE})
  mark_as_advanced(INSTALL_${var}DIR)
endforeach()

set(CMAKE_INSTALL_RPATH)
if(INSTALL_LAYOUT STREQUAL "STANDALONE")
  set(RPATH_LIBRARY_TO_LIB "${RPATH_ORIGIN}/")
  set(RPATH_BINARY_TO_LIB "${RPATH_ORIGIN}/../${INSTALL_LIBDIR}/")
else()
  set(RPATH_LIBRARY_TO_LIB "${CMAKE_INSTALL_PREFIX}/${INSTALL_LIBDIR}")
  set(RPATH_BINARY_TO_LIB "${CMAKE_INSTALL_PREFIX}/${INSTALL_LIBDIR}")
endif()

list(APPEND CMAKE_INSTALL_RPATH
	${RPATH_LIBRARY_TO_LIB}
	${RPATH_BINARY_TO_LIB})

list(REMOVE_DUPLICATES CMAKE_INSTALL_RPATH)

set(CMAKE_BUILD_WITH_INSTALL_RPATH FALSE)
set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)

if(INSTALL_SECURE_FILE_PRIVDIR)
	set(DEFAULT_SECURE_FILE_PRIV_DIR "\"${INSTALL_SECURE_FILE_PRIVDIR}\""
		CACHE INTERNAL "default --secure-file-priv directory" FORCE)
else()
	set(DEFAULT_SECURE_FILE_PRIV_DIR "\"\""
		CACHE INTERNAL "default --secure-file-priv directory" FORCE)
endif()
